package com.nfcprogram.nfc;

import java.util.StringTokenizer;

import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Parcelable;
import android.util.Log;

public class NfcManager {

	private static final String NFC_PROGRAM = "NfcManager";
	public static final boolean D = false;
	private String wifi;
	private String blu;
	
	//WIFI's Data
	private String WiFi_SSID;
	private String WiFi_preSharedKey;
	private String WiFi_KeyMgmt;
	private String WiFi_host;
	
	
	public NfcManager() {
		
	}

	public boolean readCardData(Intent intent) {
		// Parse the intent
		NdefMessage[] msgs = null;
		String action = intent.getAction();
		if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
			Parcelable[] rawMsgs = intent
					.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

			if (rawMsgs != null) {
				msgs = new NdefMessage[rawMsgs.length];
				for (int i = 0; i < rawMsgs.length; i++) {
					msgs[i] = (NdefMessage) rawMsgs[i];
				}
			} else {
				// Unknown tag type
				byte[] empty = new byte[] {};
				NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN,
						empty, empty, empty);
				NdefMessage msg = new NdefMessage(new NdefRecord[] { record });
				msgs = new NdefMessage[] { msg };
			}
			String aux = "";
			for (NdefMessage msg : msgs) {
				for (NdefRecord nRer : msg.getRecords()) {
					String value = new String(nRer.getPayload());
					aux += value.trim().substring(2);
				}
			}
			String bluTemp = "";
			String wifiTemp = "";

			StringTokenizer st = new StringTokenizer(aux, "#");
			String token1 = st.nextToken();
			wifiTemp = token1.substring(5);
			String token2 = st.nextToken();
			bluTemp = token2.substring(4);
			
			if(D) Log.d(NFC_PROGRAM, "!token1= "+token1);
			if(D) Log.d(NFC_PROGRAM, "!token2= "+token2);
			if(D) Log.d(NFC_PROGRAM, "!wifiTemp= "+wifiTemp);
			if(D) Log.d(NFC_PROGRAM, "!bluTemp= "+bluTemp);
			String valor = "isValidEnderecoBlu(bluTemp)= "
				+ isValidEnderecoBlu(bluTemp) + " lenght= " + bluTemp.length();
			valor += "isValidEnderecoWiFi(wifiTemp)= "
				+ isValidEnderecoWiFi(wifiTemp) + " lenght= " + wifiTemp.length();
			if(D) Log.d(NFC_PROGRAM, "!valor= "+valor);

			if (isValidEnderecoBlu(bluTemp) && isValidEnderecoWiFi(wifiTemp)) {
				setBlu(bluTemp);
				setWifi(wifiTemp);
				return true;
			} else {
				 valor = "isValidEnderecoBlu(bluTemp)= "
						+ isValidEnderecoBlu(bluTemp) + " bluTemp = " + bluTemp
						+ " lenght= " + bluTemp.length();
				valor += "isValidEnderecoWiFi(wifiTemp)= "
						+ isValidEnderecoWiFi(wifiTemp) + " wifiTemp= "
						+ wifiTemp + " lenght= " + wifiTemp.length();
				valor +=  "  token1= "+ token1 + " token2= "+ token2;
				if(D) Log.e(NFC_PROGRAM,"!Erro Encontrado= " + valor);
			}
		} else {
			if(D) Log.e("TAG", "Unknown intent " + intent);
		}
		return false;
	}

	public static boolean isValidEnderecoWiFi(String wifi) {
		StringTokenizer st = new StringTokenizer(wifi, "/");
		return st.countTokens() == 4;
	}

	public static boolean isValidEnderecoBlu(String endereco) {
		if (endereco != null ) {
			//return endereco.length() > 16;
			StringTokenizer st = new StringTokenizer(endereco, ":");
			return st.countTokens() == 6;
		} else {
			return false;
		}
	}

	private void setWifi(String wifi) {
		this.wifi = wifi;
		splitData(wifi);
	}

	public String getWifi() {
		return wifi;
	}

	private void setBlu(String blu) {
		this.blu = blu;
	}

	public String getBlu() {
		return blu;
	}
	
	//Split WiFi's Data
	
	private void splitData(String data) {
		StringTokenizer st = new StringTokenizer(data,"/");
		this.setWiFi_KeyMgmt(st.nextToken().trim());
		this.setWiFi_SSID(st.nextToken().trim());
		this.setWiFi_preSharedKey(st.nextToken().trim());
		this.setWiFi_host(st.nextToken().trim());
	}

	private void setWiFi_SSID(String wiFi_SSID) {
		WiFi_SSID = wiFi_SSID;
	}

	public String getWiFi_SSID() {
		return WiFi_SSID;
	}

	private void setWiFi_preSharedKey(String wiFi_preSharedKey) {
		WiFi_preSharedKey = wiFi_preSharedKey;
	}

	public String getWiFi_preSharedKey() {
		return WiFi_preSharedKey;
	}

	private void setWiFi_KeyMgmt(String wiFi_KeyMgmt) {
		WiFi_KeyMgmt = wiFi_KeyMgmt;
	}

	public String getWiFi_KeyMgmt() {
		return WiFi_KeyMgmt;
	}

	private void setWiFi_host(String wiFi_host) {
		if(D) Log.d("NfcManager", "! setWiFi_host executado, wiFi_host= "+wiFi_host);
		WiFi_host = wiFi_host;
	}

	public String getWiFi_host() {
		return WiFi_host;
	}

}
